package xx.services.util.img;


import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * <p>
 * Title: Thumbnail
 * </p>
 * <p>
 * Description: Picture Thumbnail
 * </p>
 * <p>
 * Copyright: Copyright (c) 54powerman@163.com 2005
 * </p>
 * <p>
 * Company: http://blog.sina.com.cn/u1055000490
 * </p>
 * 
 * @author 54powerman
 * @version 1.0
 */
public class Thumbnail
{
    private String storeFolder = null;

    private int width;

    private int height;

    private Image img;

    public static void main(String[] args) throws Exception
    {
        Thumbnail thum = new Thumbnail("d:/4.jpg");
        thum.resizeFix(500, 300);
    }

    /**
     * 构造函数
     * 
     * @param filePath
     *            文件地址 String
     * @throws IOException
     */
    public Thumbnail(String filePath) throws IOException
    {
        File _file = new File(filePath); // 读入文件
        img = ImageIO.read(_file); // 构造Image对象
        width = img.getWidth(null); // 得到源图宽
        height = img.getHeight(null); // 得到源图长
    }

    public Thumbnail(InputStream is) throws IOException
    {
        img = ImageIO.read(is);// 构造Image对象
        width = img.getWidth(null); // 得到源图宽
        height = img.getHeight(null); // 得到源图长
    }

    public Thumbnail(String filePath, String storeFolder) throws IOException
    {
        this.storeFolder = storeFolder;

        File _file = new File(filePath); // 读入文件
        img = ImageIO.read(_file); // 构造Image对象
        width = img.getWidth(null); // 得到源图宽
        height = img.getHeight(null); // 得到源图长
    }

    public Thumbnail(InputStream is, String storeFolder) throws IOException
    {
        this.storeFolder = storeFolder;

        img = ImageIO.read(is);// 构造Image对象
        width = img.getWidth(null); // 得到源图宽
        height = img.getHeight(null); // 得到源图长
    }

    private File createTmpFile() throws IOException
    {
        return this.storeFolder != null && !"".equals(this.storeFolder) ? File.createTempFile(
                "img", ".jpg", new File(this.storeFolder)) : File.createTempFile("img", ".jpg");
    }

    /**
     * 强制压缩/放大图片到固定的大小
     * 
     * @param w
     *            int 新宽度
     * @param h
     *            int 新高度
     * @return 返回小图文件对象
     * @throws IOException
     */
    public File resize(int w, int h) throws IOException
    {
        BufferedImage _image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        _image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图
        File file = this.createTmpFile();
        FileOutputStream out = new FileOutputStream(file); // 输出到文件流
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        encoder.encode(_image); // 近JPEG编码
        out.flush();
        out.close();
        return file;
    }

    /** */
    /**
     * 按照固定的比例缩放图片
     * 
     * @param t
     *            double 比例
     * @return 返回小图文件对象
     * @throws IOException
     */
    public File resize(double t) throws IOException
    {
        int w = (int) (width * t);
        int h = (int) (height * t);
        return resize(w, h);
    }

    /**
     * 根据最小的那边做基准，等比例缩放图片
     * 
     * @param min
     *            最小值
     * @return
     * @throws IOException
     */
    public File resize(int min) throws IOException
    {
        if (width > height)
        {
            return this.resizeByHeight(min);
        }
        else
        {
            return this.resizeByWidth(min);
        }
    }

    /** */
    /**
     * 以宽度为基准，等比例放缩图片
     * 
     * @param w
     *            int 新宽度
     * @return 返回小图文件对象
     * @throws IOException
     */
    public File resizeByWidth(int w) throws IOException
    {
        int h = (int) (height * w / width);
        if (w >= width && h >= height)
            return null;
        return resize(w, h);
    }

    /** */
    /**
     * 以高度为基准，等比例缩放图片
     * 
     * @param h
     *            int 新高度
     * @return 返回小图文件对象
     * @throws IOException
     */
    public File resizeByHeight(int h) throws IOException
    {
        int w = (int) (width * h / height);
        if (w >= width && h >= height)
            return null;
        return resize(w, h);
    }

    /** */
    /**
     * 按照最大高度限制，生成最大的等比例缩略图
     * 
     * @param w
     *            int 最大宽度
     * @param h
     *            int 最大高度
     * @return 返回小图文件对象，null表示不需要生成小图
     * @throws IOException
     */
    public File resizeFix(int w, int h) throws IOException
    {
        // 不需要生成小图
        if (w >= width && h >= height)
        {
            return null;
        }

        if (width / w > height / h)
        {
            return resizeByWidth(w);
        }
        else
        {
            return resizeByHeight(h);
        }
    }

    /**
     * 获取图片原始宽度 getSrcWidth
     */
    public int getSrcWidth()
    {
        return width;
    }

    /**
     * 获取图片原始高度 getSrcHeight
     */
    public int getSrcHeight()
    {
        return height;
    }
}